Namespace("sketch.ui");

(function(fwk, ui) {
    var Modifiers = klass(null, {
        ctrl: false,
        alt: false,
        shift: false,
        is: function(other){
            var valueOrDefault = function(value, def){
                return value === undefined ? def : value;
            };

            return this.ctrl === valueOrDefault(other.ctrl, false)
                && this.alt === valueOrDefault(other.alt, false)
                && this.shift === valueOrDefault(other.shift, false);
        }
    });

    var keys = {
        shift: 16,
        ctrl: 17,
        alt: 18
    };

    var Keyboard = klass(null, {
        _constructor: function() {
            var that = this;
            this._commandKeys = {};
            this.modifiers = new Modifiers();

            this._attachGlobalHandler("keydown", function(e) {
                if (e.keyCode === keys.ctrl) {
                    that.modifiers.ctrl = true;
                //TODO: alt sometimes does not work correctly if the browser menu is opened
                //} else if (e.keyCode === keys.alt) {
                //    that.modifiers.alt = true;
                } else if (e.keyCode === keys.shift) {
                    that.modifiers.shift = true;
                }
            });
            this._attachGlobalHandler("keyup", function(e) {
                if (e.keyCode === keys.ctrl) {
                    that.modifiers.ctrl = false;
                //} else if (e.keyCode === keys.alt) {
                //    that.modifiers.alt = false;
                } else if (e.keyCode === keys.shift) {
                    that.modifiers.shift = false;
                }
            });
        },

        _attachGlobalHandler: function(event, callback) {
            var element = document;

            if (element.addEventListener) {
                element.addEventListener(event, callback, false);
            }
            else if (element.attachEvent) {
                element.attachEvent('on' + event, callback);
            }
            else {
                element['on' + event] = callback;
            }
        },

        addShortcut: function(target, shortcutString, callback) {
            shortcut.add(shortcutString, callback, {
                type: "keydown",
                propagate: false,
                disable_in_input: true
            });
        },

        addCommandShortcut: function(target, shortcutString, commandConstructor) {
            var template = commandConstructor();

            this._commandKeys[template.name()] = shortcutString;

            this.addShortcut(target, shortcutString, function() {
                var command = commandConstructor();
                fwk.commandManager.execute(command);
            });
        },

        getShortcutKey: function(command) {
            var key = this._commandKeys[command.name()];
            if (key === undefined) {
                return null;
            }
            return key;
        }
    });

    ui.keyboard = new Keyboard();

})(sketch.framework, sketch.ui);